<?php
//
// 商铺维护人员管理
//
class Controller_Admin_ShopMgr extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type = 'shopmgr';
        parent::__construct($req, $res);

        $this->_list_where = array(
            array('utype', '=', 'merchant'),
        );

        $this->_sort_extra_columns = array(
            array('changed', DB::expr('curdate()')),
        );

        $this->_shopId = App_Shop_Manager::currentShopId();
        if ($this->_shopId > 0)
            $this->_list_where[] = array('shop_id', '=', $this->_shopId);
    }

	//默认首页
    public function action_index ()
    {
        $this->action_list();
    }

	//添加商铺管理员
    public function action_create ()
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $realName = Arr::get($post, 'realname');
            $mobile   = Arr::get($post, 'mobile');
            $password = Arr::get($post, 'password');
            $passport = Arr::get($post, 'passport');
            $shopId   = Arr::get($post, 'shop_id');

            App_Shop_Manager::create($passport, $password, $realName, $mobile, $shopId);
            $this->request->redirect('/admin/shopmgr/list');
        }
        else
        {
            parent::action_create();
        }
    }

	//修改商铺管理员
	//$nid int 商铺ID
    public function action_update ($nid)
    {
        $form = new Form_Base();
        $itemsConfig = $form->getFormItemsConfig('shopmgr');
        $ret = array();
        foreach ($itemsConfig as $config)
        {
            if (!in_array($config['input_name'], array('passport', 'password')))
                $ret[] = $config;
        }
        $this->_formItemsConfig = $ret;

        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }
        else
        {
            $data = Form_Data::detail($this->_form_type, $nid); 
            $this->_form_data = $data;
        }

        parent::action_update($nid);
    }

	// 详情
	//$nid int 商铺ID
    public function action_detail ($nid)
    {
        $data = Form_Data::detail($this->_form_type, $nid); 
        $this->_form_data = $data;

        parent::action_detail($nid);
    }

    public function detail_filter_config_data (& $arr_config)
    {
        //
    }

	//重构数据
    private function rebuild_post_data (& $post)
    {
        //
    }

	// 列表数据改造
    public function list_filter_data (& $rows)
    {
        foreach ($rows as & $rs)
        {
            //
            $shopId = $rs['shop_id'];
            $rs['shop_id'] = App_Shop::name($shopId);
        }
    }

	//操作菜单
    public function actions_by_list_row (& $row, & $menus)
    {
        //
    }
}
